package org.linuxgears.BouncingBall;

import javax.swing.*;

import org.linuxgears.BouncingBall.gui.BallCanvas;
import org.linuxgears.BouncingBall.gui.BallToolbox;
import org.linuxgears.BouncingBall.gui.InteractiveBallToolButton;
import org.linuxgears.BouncingBall.model.Ball;
import org.linuxgears.BouncingBall.model.BallController;
import org.linuxgears.BouncingBall.model.BallFactory;
import org.linuxgears.BouncingBall.model.BouncingBallModel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: khilkeol
 * Date: 30 ���� 2010
 * Time: 7:48:15
 * To change this template use File | Settings | File Templates.
 */

public class BouncingBallDemo {

    public static JFrame UI(BouncingBallModel bm, BallCanvas bv, final BallController bc) {

        JFrame appFrame = new JFrame("Bouncing Ball");
        appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        BorderLayout layout = new BorderLayout();
        appFrame.setLayout(layout);
        appFrame.add(bv, BorderLayout.PAGE_START);

        JPanel buttonPanel = new JPanel();
        
        JButton buttonReset = new JButton("Reset");
        buttonReset.addActionListener(new ActionListener() {
        	@Override
        	public void actionPerformed(ActionEvent arg0) {
        		bc.resetState();
        	}
        });
        
        JButton buttonAddBall = new JButton("Add Ball");
        buttonAddBall.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bc.addBall();
            }
        });
        
        JButton buttonDeleteBall = new JButton("Delete Ball");
        buttonDeleteBall.addActionListener(new ActionListener() {
        	@Override
	        public void actionPerformed(ActionEvent arg0) {
        		bc.deleteSelectedBall();
        	}        	
        });

        JButton buttonClose = new JButton("Close");
        buttonClose.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        JButton buttonSelectPrevious = new JButton("<<");
        buttonSelectPrevious.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				bc.selectPreviousBall();
			}
        });

        JButton buttonDeselect = new JButton("[]");
        buttonDeselect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bc.deselectBall();
            }
        });

        JButton buttonSelectNext = new JButton(">>");
        buttonSelectNext.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bc.selectNextBall();
            }
        });

        JButton buttonPause = new JButton("Pause / Resume");
        buttonPause.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bc.togglePause();
            }
        });

        buttonPanel.add(buttonReset);
        buttonPanel.add(buttonPause);
        
        buttonPanel.add(buttonAddBall);
        buttonPanel.add(buttonDeleteBall);
        buttonPanel.add(buttonClose);

        buttonPanel.add(buttonSelectPrevious);
        buttonPanel.add(buttonDeselect);
        buttonPanel.add(buttonSelectNext);

        appFrame.add(buttonPanel, BorderLayout.PAGE_END);

        BallToolbox toolbox = new BallToolbox(bc);

        toolbox.setLayout(new BoxLayout(toolbox, BoxLayout.X_AXIS));

        for (Ball ball: BallFactory.getProductSet()) {
            toolbox.addBallButton(ball);
        }

        
        // interactive ball pedal
        toolbox.add(
        		new InteractiveBallToolButton(BallFactory.getInstance().createColoredBall(), bc)
        		);
        
        appFrame.add(toolbox, BorderLayout.LINE_START);

        appFrame.pack();

        return appFrame;
    };

    public static void main(String... args) {
/*        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        final BouncingBallModel bm = new BouncingBallModel();     	// model
        final BallController bc = new BallController(bm);   		// controller
        final BallCanvas bv = new BallCanvas(bm, bc);              // view
        

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                UI(bm, bv, bc).setVisible(true);
            }
        });
    }
}